
const core = new GameEngineCore();
setTimeout(function () {
    core.start();
}, 100);


function ease(advancedTime, totalTime) {

    // return Mat h.sin(advancedTime / totalTime * Math.PI) + 1;
    let scale = advancedTime / totalTime;
    if (scale > 1) {
        scale = 1;
    }
    return scale;
}

const radius = 10;

const img1 = new ImageRenderer();
const img2 = new ImageRenderer();
const img3 = new ImageRenderer();
const img4 = new ImageRenderer();
const img5 = new ImageRenderer();
const img6 = new ImageRenderer();

const backGround = new drawBackGoound();
const leftText = new drawText();
const rightText = new drawText();
const redImp = new drawImp("redImp");
const orangeImp = new drawImp("orangeImp");
const pinkImp = new drawImp("pinkImp");
const yellowImp = new drawImp("yellowImp");
const purpleImp = new drawImp("purpleImp");
const rightTR = new drawRTR();
const leftTR = new drawLTR();
const rightConcava = new drawRConcave();
const leftConcava = new drawLConcave();
const downT = new drawDT();
const upT = new drawUT();
const rightT = new drawRT();
const leftT = new drawLT();
const player = new drawPlayer("Player1");


const otherRender = new drawOther();

const roundR1 = new drawRR();
const roundR2 = new drawRR();
const roundR3 = new drawRR();
const roundR4 = new drawRR();
const roundR5 = new drawRR();
const roundR6 = new drawRR();
const roundR7 = new drawRR();
const roundR8 = new drawRR();
const roundR9 = new drawRR();

const BackgroundContainer = new DisplayObjectContainer();
const RoundRContainer = new DisplayObjectContainer();
const PlayerContainer = new DisplayObjectContainer();
const ImpContainer = new DisplayObjectContainer();
const ImageContainer = new DisplayObjectContainer();
const TextContainer = new DisplayObjectContainer();



core.onStart = function () {

    backGround.color = "black";

    leftText.x = 15;
    leftText.y = 200;
    leftText.text = "Pac Man";
    leftText.color = "yellow";

    rightText.x = 320;
    rightText.y = 200;
    rightText.text = "Round 5";
    rightText.color = "yellow";

    redImp.x = 175;
    redImp.y = 200;
    redImp.righteye = 2;
    redImp.lefteye = -6;
    redImp.color = "red";

    orangeImp.x = 200;
    orangeImp.y = 200;
    orangeImp.righteye = -2;
    orangeImp.lefteye = 2;
    orangeImp.color = "orange";
    orangeImp.movedirection = 0;
    orangeImp.range[0] = 195;
    orangeImp.range[1] = 205;
    orangeImp.speed = 0.1;

    pinkImp.x = 225;
    pinkImp.y = 196;
    pinkImp.righteye = 8;
    pinkImp.lefteye = 0;
    pinkImp.color = "pink";
    pinkImp.movedirection = 1;
    pinkImp.range[0] = 190;
    pinkImp.range[1] = 205;
    pinkImp.speed = 0.2;

    yellowImp.x = 175;
    yellowImp.y = 30;
    yellowImp.righteye = -8;
    yellowImp.lefteye = 0;
    yellowImp.color = "yellow";
    yellowImp.movedirection = 1;
    yellowImp.range[0] = 20;
    yellowImp.range[1] = 70;
    yellowImp.speed = 0.4;

    purpleImp.x = 254;
    purpleImp.y = 170;
    purpleImp.righteye = -8;
    purpleImp.lefteye = 2;
    purpleImp.color = "purple";
    purpleImp.movedirection = 1;
    purpleImp.range[0] = 170;
    purpleImp.range[1] = 270;
    purpleImp.speed = 0.5;

    rightTR.x = 275;
    rightTR.y = 340;
    rightTR.color = "blue";

    leftTR.x = 125;
    leftTR.y = 340;
    leftTR.color = "blue";

    rightConcava.x = 320;
    rightConcava.y = 320;
    rightConcava.color = "blue";

    leftConcava.x = 80;
    leftConcava.y = 320;
    leftConcava.color = "blue";

    downT.x = 200;
    downT.y = 100;
    downT.color = "blue";

    upT.x = 200;
    upT.y = 360;
    upT.color = "blue";

    rightT.x = 125;
    rightT.y = 140;
    rightT.color = "blue";


    leftT.x = 275;
    leftT.y = 140;
    leftT.color = "blue";

    player.x = 25;
    player.y = 25;
    player.color = "yellow";

    roundR1.x = 200;
    roundR1.y = 270;
    roundR1.w = 80;
    roundR1.l = 20;
    roundR1.color = "blue";

    roundR2.x = 65;
    roundR2.y = 50;
    roundR2.w = 60;
    roundR2.l = 30;
    roundR2.color = "blue";

    roundR3.x = 140;
    roundR3.y = 50;
    roundR3.w = 40;
    roundR3.l = 30;
    roundR3.color = "blue";

    roundR4.x = 260;
    roundR4.y = 50;
    roundR4.w = 40;
    roundR4.l = 30;
    roundR4.color = "blue";

    roundR5.x = 335;
    roundR5.y = 50;
    roundR5.w = 60;
    roundR5.l = 30;
    roundR5.color = "blue";

    roundR6.x = 65;
    roundR6.y = 100;
    roundR6.w = 60;
    roundR6.l = 20;
    roundR6.color = "blue";

    roundR7.x = 335;
    roundR7.y = 100;
    roundR7.w = 60;
    roundR7.l = 20;
    roundR7.color = "blue";

    roundR8.x = 125;
    roundR8.y = 250;
    roundR8.w = 20;
    roundR8.l = 60;
    roundR8.color = "blue";

    roundR9.x = 275;
    roundR9.y = 250;
    roundR9.w = 20;
    roundR9.l = 60;
    roundR9.color = "blue";

    img1.image = getImageUrl('../image/tex2.jpg');
    img1.x = 225;
    img1.y = 210;
    img1.w = 70;
    img1.l = 120;

    img2.image = getImageUrl('../image/tex1.jpg');
    img2.x = 115;
    img2.y = 210;
    img2.w = 70;
    img2.l = 130;

    img3.image = getImageUrl('../image/tex1.jpg');
    img3.x = 235;
    img3.y = 90;
    img3.w = 70;
    img3.l = 120;

    img4.image = getImageUrl('../image/tex2.jpg');
    img4.x = 105;
    img4.y = 90;
    img4.w = 70;
    img4.l = 130;

    img5.image = getImageUrl('../image/tex3.jpg');
    img5.x = 160;
    img5.y = 250;
    img5.w = 80;
    img5.l = 140;

    img6.image = getImageUrl('../image/tex4.jpg');
    img6.x = 160;
    img6.y = 50;
    img6.w = 80;
    img6.l = 140;


    core.root.addChild(BackgroundContainer);
    core.root.addChild(ImpContainer);
    core.root.addChild(PlayerContainer);
    core.root.addChild(ImageContainer);
    core.root.addChild(RoundRContainer);
    core.root.addChild(TextContainer);

    BackgroundContainer.x = BackgroundContainer.y = 0;
    ImageContainer.x = ImageContainer.y = 0;
    PlayerContainer.x = PlayerContainer.y = 0;
    ImpContainer.x = ImpContainer.y = 0;
    RoundRContainer.x = RoundRContainer.y = 0;
    TextContainer.x = TextContainer.y = 0;

    BackgroundContainer.alpha = ImpContainer.alpha = PlayerContainer.alpha = ImageContainer.alpha = RoundRContainer.alpha = TextContainer.alpha = 1.0;


    BackgroundContainer.addChild(backGround);
    BackgroundContainer.addChild(otherRender);

    ImpContainer.addChild(purpleImp);
    ImpContainer.addChild(orangeImp);
    ImpContainer.addChild(yellowImp);
    ImpContainer.addChild(pinkImp);
    ImpContainer.addChild(redImp);      
    ImpContainer.addBehaviour(new ClickContainerBehaviour());
  
 
    PlayerContainer.addChild(player);
    PlayerContainer.addBehaviour(new ClickContainerBehaviour());
   
    RoundRContainer.addChild(roundR1);
    RoundRContainer.addChild(roundR2);
    RoundRContainer.addChild(roundR3);
    RoundRContainer.addChild(roundR4);
    RoundRContainer.addChild(roundR5);
    RoundRContainer.addChild(roundR6);
    RoundRContainer.addChild(roundR7);
    RoundRContainer.addChild(roundR8);
    RoundRContainer.addChild(roundR9);  
    RoundRContainer.addChild(leftTR);
    RoundRContainer.addChild(rightTR);
    RoundRContainer.addChild(leftConcava);
    RoundRContainer.addChild(rightConcava);
    RoundRContainer.addChild(upT);
    RoundRContainer.addChild(downT);
    RoundRContainer.addChild(leftT);
    RoundRContainer.addChild(rightT);

    TextContainer.addChild(rightText);
    TextContainer.addChild(leftText);
    
   
    /*
    core.saveCanvas();
    drawClip();
    ImageContainer.addChild(img1);
    ImageContainer.addChild(img2);
    ImageContainer.addChild(img3);
    ImageContainer.addChild(img4);
    ImageContainer.addChild(img5);
    ImageContainer.addChild(img6);
    core.restoreCanvas();
    */
}



core.onUpdate = function (duringTime) {
    purpleImp.move();
    orangeImp.move();
    pinkImp.move();
    yellowImp.move();

}

core.onDestroy = function () {

}


class ClickContainerBehaviour extends Behaviour {
    
    //定义点击行为？
    //一个move只能作用于一个对象？ 一个move对应一个时间片
    onStart(){
        const move1 = new MoveBehaviour();
        const move3 = new MoveBehaviour();
      
        move1.time = 10000;
        move1.targetX = 300;
        move3.time = 10000; 
        move3.targetX = 300;

        PlayerContainer.onClick = function ()
        {
        PlayerContainer.addBehaviour(move3);
        }


        purpleImp.onClick=function()
        {
            purpleImp.addBehaviour(move3);
        }
        
       redImp.onClick = function()
        {
            redImp.addBehaviour(move1);
        }
    
    }
}



class MoveBehaviour extends Behaviour {


    time = 1000;

    targetX = 0;


    gameObject;

    currentTime = 0;

    initX;

    $isDestroy = false;

    constructor() {
        super();
    }

    onStart() {
        this.initX = this.gameObject.x;
    }

    onUpdate(duringTime) {
        
        this.currentTime += duringTime;
        let timeRatio = this.currentTime / this.time;
        if (timeRatio > 1) {
            timeRatio = 1;
        }
        const needToMoveX = this.targetX - this.initX;
        this.gameObject.x = this.initX + timeRatio * needToMoveX;
        if (timeRatio == 1) {
            this.gameObject.removeBehaviour(this);
        }
    }

    onDestroy() {

    }
}

class StraightMoveBehaviour extends MoveBehaviour { }

window.addEventListener("keydown", function (e) {
    if (event.keyCode == 39) {
        PlayerContainer.x += 10;
        player.rotation = 0;
    }
    if (event.keyCode == 37) {
        PlayerContainer.x -= 10;
        player.rotation = 180;
    }

    //注意坐标原点在左上角，y轴正方向是向下。
    if (event.keyCode == 40) {
        PlayerContainer.y += 10;
        player.rotation = 90;
    }
    if (event.keyCode == 38) {
        PlayerContainer.y -= 10;
        player.rotation = 270;
    }
})

window.addEventListener('click', function (e) {

    const mouseGlobalX = e.offsetX;
    const mouseGlobalY = e.offsetY;
    const point = { x: mouseGlobalX, y: mouseGlobalY };
    core.hitTest(point);

})

